"""
    User API
    @author: Jean-Lou Dupont
"""

import os
import sys
import logging
import string

import wsgiref.handlers
from google.appengine.ext           import webapp
from google.appengine.ext.webapp    import template
from google.appengine.api           import users

from libs.user.prefs import UserPrefs

class UserPrefsApiHandler(webapp.RequestHandler):
    
    def get(self):
        user = users.get_current_user()

        if not user:
            self.response.set_status( 401 )
            return

        up = UserPrefs.create( user )

        template_values = { 'uid':up.uid }
        
        try:
            path = os.path.join(os.path.dirname(__file__), 'templates/userprefs.json')  #TODO hardened...  
            self.response.out.write(template.render(path, template_values))
            #self.response.headers["Content-Type"] = "application/json"
        except:
            self.response.set_status( 404 )
        
#========================================================================================================
def main():
  application = webapp.WSGIApplication([('/api/userprefs/',UserPrefsApiHandler) ], debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()        